#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Onur Yerlikaya <onur@php.net>

import os,sys
class ZF:
        def printcolor(self,txt):
            os.system("echo -en \"\033[37m\033["+str(txt)+"\033[0m\"");
            print
        def about(self):
            self.printcolor("------------------------------------------------")
            self.printcolor("        ___    _          __       ____         ")
            self.printcolor("   ___ / _/   (_)__  ___ / /____ _/ / /__ ____  ")
            self.printcolor("  /_ // _/   / / _ \(_-</ __/ _ \`/ / / -_) __/  ")
            self.printcolor("  /__/_/    /_/_//_/___/\__/\_,_/_/_/\__/_/     ")
            self.printcolor(" -----------------------------------------------")
            self.printcolor("  Written by Onur Yerlikaya <onur@php.net>      ")
            self.printcolor(" -----------------------------------------------")


        def create_dir(self,dirname,param=0):
            if param == 0:
                dirname = str(self.main_dir)+"/"+dirname
            if (os.path.exists(dirname)):
                os.system("rm -rf "+str(dirname))
                print str(dirname)+" Finded and Deleted. "
            os.mkdir(dirname)
            print str(dirname)+" Directory Created. "

        def install(self):
            self.create_dir(self.extract_dir,1)
            self.create_dir(str(self.extract_dir)+"/tmp",1)
            self.create_dir("library")
            os.system("cd "+str(self.extract_dir)+" && wget -c http://framework.zend.com/releases/ZendFramework-1.0.1/ZendFramework-1.0.1.tar.gz")
            os.system("tar zxf "+str(self.extract_dir)+"/ZendFramework-1.0.1.tar.gz -C "+str(self.extract_dir)+"/tmp")
            print "ZF Archive Extracting..."
            os.system("mv "+str(self.extract_dir)+"/tmp/ZendFramework-1.0.1/library "+str(main_dir)+"/library")
            print "ZF Library Moved To "+str(main_dir)+"/library"
            os.system("rm -rf "+str(self.extract_dir)+"/tmp")
            print "Temporary Directory Deleted. "

        def __init__(self,main_dir,docroot,extract_dir):
            self.about()
            print "ZF Installer Initialized."
            print "Creating Filesystem..."
            # global style
            self.main_dir = main_dir
            self.docroot = docroot
            self.extract_dir = extract_dir
            # create dirs
            self.create_dir(main_dir,1)
            self.create_dir(docroot)
            self.create_dir("app")
            self.create_dir("app/models")
            self.create_dir("app/views")
            self.create_dir("app/views/scripts")
            self.create_dir("app/views/scripts/index")
            self.create_dir("app/views/helpers")
            self.create_dir("app/views/filters")
            self.create_dir("app/controllers")

        def createHtaccess(self):
            print "Creating Htaccess File..."
            f = open(str(self.main_dir)+"/"+str(self.docroot)+"/.htaccess",'w')
            f.write("RewriteEngine on\n")
            f.write("RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php")
            f.close()

        def createRedirectIndex(self):
            f = open(str(self.main_dir)+"/index.php",'w')
            f.write("<?php\n")
            f.write("/* Generated with zf.py */\n")
            f.write("header(\"Location: "+str(self.docroot)+"/index.php\");\n")
            f.write("?>")
            f.close()

        def createDocrootIndex(self):
            print "Creating Sample Application Index File..."
            f = open(str(self.main_dir)+"/"+str(self.docroot)+"/index.php",'w')
            f.write("<?php\n")
            f.write("/* Generated with zf.py */\n")
            f.write("error_reporting( E_ALL | E_STRICT );\n")
            f.write("$zfLibraryDir = dirname(dirname(__FILE__)) . \'/library\';\n")
            f.write("set_include_path(\n")
            f.write("    $zfLibraryDir\n")
            f.write("    . PATH_SEPARATOR\n");
            f.write("    . get_include_path()\n")
            f.write(");\n")
            f.write("require_once \'Zend/Loader.php\';\n")
            f.write("Zend_Loader::registerAutoload();\n")
            f.write("$appDir = dirname(dirname(__FILE__)) . \'/app\';\n")
            f.write("set_include_path(\n")
            f.write("    $appDir . \'/models\'\n")
            f.write("    . PATH_SEPARATOR\n");
            f.write("    . get_include_path()\n")
            f.write(");\n")
            f.write("Zend_Controller_Front::run(\"$appDir/controllers\");")
            f.write("?>")
            f.close()

        def createIndexController(self):
            print "Creating Sample Index Controller File..."
            f = open(str(self.main_dir)+"/app/controllers/IndexController.php",'w')
            f.write("<?php\n")
            f.write("/* Generated with zf.py */\n")
            f.write("class IndexController extends Zend_Controller_Action\n")
            f.write("{\n")
            f.write("    public function indexAction()\n")
            f.write("    {\n")
            f.write("    }\n")
            f.write("}\n")
            f.write("?>")
            f.close()

        def createIndexPhtml(self):
            print "Creating Sample Index Phtml File.."
            f = open(str(self.main_dir)+"/app/views/scripts/index/index.phtml",'w')
            f.write("<!DOCTYPE html\n")
            f.write("PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n")
            f.write("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n")
            f.write("<!-- Generated with zf.py -->\n")
            f.write("<html>\n")
            f.write("<head>\n")
            f.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n")
            f.write("<title>My first Zend Framework App</title>\n")
            f.write("</head>\n")
            f.write("<body>\n")
            f.write("<h1>Hello, World!</h1>\n")
            f.write("</body>\n")
            f.write("</html>")
            f.close()

if len(sys.argv) > 3:
    main_dir    = sys.argv[1]
    docroot     = sys.argv[2]
    extract_dir = sys.argv[3]
else:
    print "\
    Usage \n\
        ./zf.py MAIN_DIR DOCROOT EXTRACT_DIRECTORY"
    sys.exit()

zf = ZF(main_dir,docroot,extract_dir)

try:
    zf.install()
    zf.createHtaccess()
    zf.createRedirectIndex()
    zf.createDocrootIndex()
    zf.createIndexController()
    zf.createIndexPhtml()
except KeyboardInterrupt:
    print "Failed!"
    sys.exit(1)
